using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace TTBox
{
public class S3000GGSMModem
{
    // Fields
    private SerialPort port = null;
    private AutoResetEvent receiveNow = new AutoResetEvent(false);
    private string respAT = "";

    // Methods
    public S3000GGSMModem()
    {
        try
        {
            this.port = this.OpenPort(GPSData.comportAT);
            this.respAT = this.ExecCommand("AT", 300, "No modem connected at " + GPSData.comportAT + ".");
            if (this.respAT.EndsWith("\r\nOK\r\n"))
            {
                this.respAT = "";
                this.respAT = this.ExecCommand("ATE0", 300, "Failed to set echo off.");
                if (this.respAT.EndsWith("\r\nOK\r\n"))
                {
                    this.respAT = "";
                    this.respAT = this.ExecCommand("AT+CGSN", 300, "Failed to get imeino.");
                    if (this.respAT.EndsWith("\r\nOK\r\n"))
                    {
                        string str = "";
                        str = this.respAT.Substring(this.respAT.IndexOf("\r\n") + 2, 15);
                        if (str.Length == 15)
                        {
                            GPSData.imeiNO = str + "F";
                            GPSData.imsiNO = str + "F";
                            GPSData.PNDSimNo = str + "F";
                        }
                        this.respAT = "";
                        this.respAT = this.ExecCommand("AT+CPIN=\"0925\"", 300, "Failed to set SimCard ready.");
                        if (this.respAT.EndsWith("\r\nOK\r\n"))
                        {
                            this.respAT = "";
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            if (this.port != null)
            {
                this.ClosePort(this.port);
                this.port = null;
            }
        }
    }

    private void ClosePort(SerialPort port)
    {
        port.Close();
        port.DataReceived -= new SerialDataReceivedEventHandler(this.port_DataReceived);
    }

    private string ExecCommand(string command, int responseTimeout, string errorMessage)
    {
        string str2;
        try
        {
            this.port.DiscardOutBuffer();
            this.port.DiscardInBuffer();
            this.receiveNow.Reset();
            this.port.Write(command + "\r");
            string str = this.ReadResponse(responseTimeout);
            if ((str.Length == 0) || !str.EndsWith("\r\nOK\r\n"))
            {
                throw new ApplicationException("No success message was received.");
            }
            str2 = str;
        }
        catch (Exception exception)
        {
            throw new ApplicationException(errorMessage, exception);
        }
        return str2;
    }

    private SerialPort OpenPort(string portName)
    {
        SerialPort port = new SerialPort();
        port.PortName = portName;
        port.BaudRate = Convert.ToInt32(GPSData.baudrateAT);
        port.DataBits = 8;
        port.StopBits = StopBits.One;
        port.Parity = Parity.None;
        port.ReadTimeout = 300;
        port.WriteTimeout = 300;
        port.DataReceived += new SerialDataReceivedEventHandler(this.port_DataReceived);
        port.Open();
        port.DtrEnable = true;
        port.RtsEnable = true;
        return port;
    }

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        this.receiveNow.Set();
    }

    private string ReadResponse(int timeout)
    {
        string str = string.Empty;
        do
        {
            if (this.receiveNow.WaitOne(timeout, false))
            {
                string str2 = this.port.ReadExisting();
                str = str + str2;
            }
            else
            {
                if (str.Length > 0)
                {
                    throw new ApplicationException("Response received is incomplete.");
                }
                throw new ApplicationException("No data received from phone.");
            }
        }
        while (!str.EndsWith("\r\nOK\r\n") && !str.EndsWith("\r\nERROR\r\n"));
        return str;
    }
}

 
 
}
